﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using Microsoft.Win32;
using System.Xml;

namespace DALDWF.App_Code
{
    class clsSetting
    {
        #region --- Setting System
        public clsSystem SetUp()
        {
            clsSystem obj = new clsSystem();
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("data.xml");

                XmlNodeList xnList = doc.SelectNodes("/setting");
                foreach (XmlNode xn in xnList)
                {
                    obj.Location = xn["location"].InnerText;
                    string updateTime = xn["update_time"].InnerText;
                    obj.UpdateTime = Convert.ToInt32(updateTime);
                    obj.Collection = xn["collection"].InnerText;
                    string startUp = xn["start_up"].InnerText;
                    if (startUp.Equals("true"))
                    {
                        obj.StartUp = true;
                    }
                    else { obj.StartUp = false; }

                }
            }
            catch(Exception crap )
            {
                throw crap;
            }
            return obj;
        }

        public static bool NewConfigFile()
        {
            bool rs = false;

            return rs;
        }
        #endregion --- Setting System

        #region --- GUI Setting
        public static string CustomFontFamilly = "Arial";
        public static float CustomFontSize = 10.0f;
        public static float HeadingFontSize = 16.0f;

        public static Font CustomFontStyle()
        {
            Font newFont = new Font("Arial", 10.0f);
            return newFont;
        }
        #endregion --- GUI Setting

        #region --- Save and Load Weather Update Time Setting
        public static void SaveUpdateTime(int minutes)
        {
            int _min = 60 * minutes;
            
            StreamWriter _log;

            if (!File.Exists("mwf.da"))
            {
                string txtLog = Convert.ToString(_min);
                _log = new StreamWriter("mwf.da");
                _log.Write(txtLog);
                _log.Close();
            }
            
        }

        public static int LoadUpdateTime()
        {
            int _updTime=0;

            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            StreamReader sr = new StreamReader("mwf.da");
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                _updTime = Convert.ToInt32(line);
            }

            return _updTime;
        }
        #endregion --- Save and Load Weather Setting

        #region --- Save and Load City
        public static void SaveCity(string city)
        {
            StreamWriter _log;

            if (!File.Exists("mwfct.da"))
            {
                string txtLog = city;
                _log = new StreamWriter("mwfct.da");
                _log.Write(txtLog);
                _log.Close();
            }
        }

        public static string LoadCity()
        {
            StreamReader sr = new StreamReader("mwfct.da");
            String line;
            string city = "";

            while ((line = sr.ReadLine()) != null)
            {
                city = line;
            }

            return city;
        }
        #endregion --- Save and Load City

        #region --- Start with Windows
        public static void StartWithWindows(bool status)
        {
            RegistryKey regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            if (status == true)
            {
                regKey.SetValue("DALDWF.exe", Application.ExecutablePath.ToString());
            }
            else
            {
                regKey.DeleteValue("DALDWF.exe", false);
            }
        }
        #endregion --- Start with Windows
    }

}
